package com.example.facecomparedemo.Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

@WebServlet("/api/*")
public class ImageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part filePart = req.getPart("file");

        // 获取上传文件的名称
        String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();

        // 获取上传文件的输入流
        InputStream fileContent = filePart.getInputStream();

        // 将文件保存到服务器中的指定路径
        Files.copy(fileContent, Paths.get("D:/upload/" + fileName));
        System.out.println("上次成功!");

        resp.getWriter().println("File uploaded successfully");
    }
}
